package RozetkaTesting;

import RozetkaPages.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import util.ReportWriter;

import static org.testng.Assert.fail;

/**
* Created by Admin on 02.04.2014.
*/
public class RozetkaTestSteps extends BasicSetUp{

    public void openHomePage(BasePage page)
    {
        page.open();
        Assert.assertTrue(page.isOpened());
        ReportWriter.info("HomePage is opened!");
    }

    public void clickOnMainMenuTabs(BasePage page, String tabSelector, String breadcrumbs, String newPageTitle) throws InterruptedException
    {
        try{
            page.clickOnMenuItem(tabSelector);
            String newPage = driver.findElement(By.cssSelector(breadcrumbs)).getText();
            Assert.assertEquals(newPageTitle, newPage);
            ReportWriter.logElementPresent(newPageTitle);
        } catch (Exception e){
            ReportWriter.logElementNotPresent(newPageTitle);
        }
    }

    public void userLogin(BasePage page,String user, String password, String testType)
    {
        try{
            if(testType == "positive"){
                page.login(user, password);
                userIsLoggedIn(page);
                ReportWriter.info("User successfully logged in!");
            }
            else if(testType == "negative"){
                page.login(user, password);
                WebElement error = driver.findElement(By.cssSelector(".message.code4"));
                String errorMessage = error.getText();
                Assert.assertTrue(error.isDisplayed());
                ReportWriter.info("User is not logged in because of wrong User Credentials! Error message:" + errorMessage);
            }
        } catch (Exception e)  {
            ReportWriter.error("Authorization test cannot be executed!");
        }
    }

    public void userIsLoggedIn(BasePage page)
    {
        try{
            {
                page.isLoggedIn();
                ReportWriter.info("User is logged in!");
            }
        } catch (Exception e){
            ReportWriter.error("User is not logged in!");
        }
    }

    public void userLogout(BasePage page, String signInPopUp) {
        try {
            page.logout();
            WebElement signIn = driver.findElement(By.cssSelector(signInPopUp));
            Assert.assertTrue(signIn.isDisplayed());
            ReportWriter.info("User is logged out!");
        } catch (Exception e) {
            ReportWriter.error("Logout cannot be performed!");
        }
    }

    public void searchForProduct(BasePage page, String productTitle, String product, String fullProductTitle)
    {
        try{
            page.search(productTitle);
            String resultPage = driver.findElement(By.xpath(product)).getText();
            Assert.assertEquals(fullProductTitle, resultPage);
            ReportWriter.logElementPresent(fullProductTitle);
        } catch (Exception e){
            ReportWriter.logElementNotPresent(fullProductTitle);
        }
    }

    public void makeOrder(BasketPage page, String productTitle, String count)
    {
        page.openBasket();
        try{
            Assert.assertTrue(driver.findElement(By.cssSelector(".cell.detail>.title>a"))
                    .getText().contains(productTitle));
            ReportWriter.info("Product is in the Basket!");
        } catch (Exception e){
            ReportWriter.info("Product is not added to the Basket!");
        }

        page.order(count);
        int x = Integer.parseInt(count);
        if (x > 0){
            Assert.assertTrue(driver.findElement(By.cssSelector(".check-title")).isDisplayed());
            ReportWriter.info("Order application is opened!");
        } else if (x == 0){
        Assert.assertTrue(driver.findElement(By.cssSelector(".check-title")).isDisplayed(), ReportWriter.error("Test is failed!"));
        }
    }
}
